package top.xana.acg.anime.repo

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import top.xana.acg.anime.SearchSource
import top.xana.acg.anime.database.AnimeDao
import top.xana.acg.anime.model.Anime
import top.xana.acg.anime.model.Banner
import top.xana.acg.anime.network.AnimeApi
import javax.inject.Inject

/**
 * @author xana
 * @date 2022/10/30 0030 12:29
 * @email cv4096@qq.com
 **/

class AnimeRepo @Inject constructor(
    private val api: AnimeApi,
    private val dao: AnimeDao,
    private val ioDispatcher: CoroutineDispatcher
) {
    companion object {
        const val TAG = "AnimeRepo"
    }

    fun loadBanner() = flow {
        val banners = listOf<Banner>()
        emit(banners)
    }.flowOn(Dispatchers.IO)

    fun loadHot() = flow {
        val hotAnimeList = listOf<Anime>()
        emit(hotAnimeList)
    }.flowOn(Dispatchers.IO)

    fun loadCollection() = flow {
        emit(listOf<Anime>())
    }.flowOn(Dispatchers.IO)

    fun loadHomeBody() = flow {
        val homeList = api.fetHomeBody()
        emit(homeList)
    }.flowOn(Dispatchers.IO)

    fun search(
        searchSource: SearchSource,
        keyword: String,
        page: Int
    ) = flow {
        val animeList = api.search(searchSource, keyword, page)
        emit(animeList)
    }.flowOn(Dispatchers.IO)

    fun loadDetail(
        searchSource: SearchSource,
        uri: String
    ) = flow {
        val realAnime = api.fetchDetail(searchSource, uri)
        emit(realAnime)
    }.flowOn(Dispatchers.IO)


    fun loadSource(
        searchSource: SearchSource,
        uri: String
    ) = flow {
        val sourceList = api.fetchSource(searchSource, uri)
        emit(sourceList)
    }.flowOn(Dispatchers.IO)

    fun loadFavor(
        searchSource: SearchSource,
        uri: String
    ) = flow {
        val favorList = api.fetchFavor(searchSource, uri)
        emit(favorList)
    }.flowOn(Dispatchers.IO)

    fun loadPlayUrl(
        searchSource: SearchSource,
        uri: String
    ) = flow {
        val playUrl = api.fetchPlayUrl(searchSource, uri)
        emit(playUrl)
    }.flowOn(Dispatchers.IO)

    fun loadTimeline() = flow {
        val weekAnime = api.fetchTimeline()
        emit(weekAnime)
    }.flowOn(Dispatchers.IO)

    fun loadHomeCate(i: Int, page: Int = 1) = flow {
        val animeList = api.fetchCategory(i, p = page)
        emit(animeList)
    }.flowOn(Dispatchers.IO)
}